package com.xiaohao.springbootadmintemplate.config.securitycontext;

import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

@NoArgsConstructor
@Data
@Accessors(chain = true)
public class SecurityContextUserDetails implements UserDetails {

    //设置用户名以及密码
    private String username;
    private String password;
    //用户的权限
    private Collection<? extends GrantedAuthority> authorities;

    //构造函数
    public SecurityContextUserDetails(String username, String password, List<String> roles) {
        this.username = username;
        this.password = password;
        this.authorities = roles.stream().map(SimpleGrantedAuthority::new).collect(Collectors.toList());
    }

    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    // 账号是否未锁定，默认是false
    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    // 账号凭证是否未过期，默认是false
    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    // 这个有点抽象不会翻译，默认也是false
    @Override
    public boolean isEnabled() {
        return true;
    }
}

